package cn.edu.dgut.filter;

//import com.sun.javaws.progress.PreloaderDelegate;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证是否已经登录
 */

@WebFilter("/*")
public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //获取session
        Object isLogin = request.getSession().getAttribute("isLogin");
        String uri = request.getRequestURI();

        if(uri.contains("/css/")||uri.contains("/img/")||uri.contains("/js/")||uri.contains("/login/dgut")||uri.contains("/logoutServlet")
                ||uri.contains("/login.jsp")||uri.contains("/index.jsp")||uri.contains("/register.html")) {
            chain.doFilter(req, resp);
        }else{
            if(isLogin==null){
                //访问其他页面时，若没有登录，则重定向到首页index.jsp进行登录
                response.sendRedirect("/index.jsp");
            }else{
                chain.doFilter(req, resp);
            }
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
